<?php
/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 2017/6/23
 * Time: 上午11:13
 */

while (true) {
    try {
        deletefile();
    } catch (Exception $e) {
        echo "home/loops/delete  异常。。。";
    }
    sleep(3600 * 24);// 一天执行一次
}


function deletefile() {
    $dir =  dirname(dirname(dirname(dirname(__FILE__)))).'/upload/msg_file/';
    $second = '86400';
    $file_type = 'dat';
    $files = scandir($dir);
    foreach ($files as $filename) {
        $worn_time = filectime($dir.$filename);
        $new_time = time();
        $time = $new_time - $worn_time;

        if($filename!='.' && $filename!='..'){
            if ($time > $second) {
                if (false != stristr($filename, $file_type)) {
                    $flag = unlink($dir.$filename);
                    if ($flag) {
                        echo ("Deleted $filename success");
                    } else {
                        echo ("Error deleting $filename");
                    }
                }

            }

        }
    }
}